package com.bz.xtcx.manager.service.impl;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.CmsExchanges;
import com.bz.xtcx.manager.entity.CmsFloatBox;
import com.bz.xtcx.manager.entity.User;
import com.bz.xtcx.manager.mapper.CmsFileMapper;
import com.bz.xtcx.manager.mapper.CmsFloatBoxMapper;
import com.bz.xtcx.manager.service.ICmsCategoryService;
import com.bz.xtcx.manager.service.ICmsFloatBoxService;
import com.bz.xtcx.manager.vo.VoQueryExchanges;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ygl
 * @create 2018-11-08
 * @DESCRIPTION
 **/
@Service
public class CmsFloatBoxService implements ICmsFloatBoxService {

    @Autowired
    private CmsFloatBoxMapper cmsFloatBoxMapper;

    @Autowired
    private HttpServletRequest request;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public User getUser(){
        User user = null;
        String token = request.getHeader("token");
        if(token == null) {
            return user;
        }
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj != null){
            user = ((User)obj);
        }
        return user;
    }

    @Override
    public String getUserName(){
        String username = "auto";
        String token = request.getHeader("token");
        if(token == null) {
            return username;
        }
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj != null){
            username = ((User)obj).getUserName();
        }
        return username;
    }

    @Override
    public String getUserId(){
        String userId = null;
        String token = request.getHeader("token");
        if(token == null) {
            return userId;
        }
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj != null){
            userId = ((User)obj).getUserId();
        }
        return userId;
    }

    @Override
    public void insert(CmsFloatBox obj) {
        cmsFloatBoxMapper.insert(obj);
    }

    @Override
    public void delete(String id) {
        cmsFloatBoxMapper.delete(id);
    }

    @Override
    public void update(CmsFloatBox obj) {
        cmsFloatBoxMapper.update(obj);
    }

    @Override
    public CmsFloatBox selectById(String id) {
        return cmsFloatBoxMapper.selectById(id);
    }

    @Override
    public PageInfo<CmsFloatBox> getPageByCondition(String content,int pageNum, int pageSize, String orderBy) throws XtcxException {
        User u = getUser();
        if (null == u) {
            throw new XtcxException ("登录已超时，请重新登录");
        }
        Page<CmsFloatBox> page = PageHelper.startPage(pageNum, pageSize);
        if(StringUtils.isEmpty(orderBy)) {
            PageHelper.orderBy("create_time desc");
        }else {
            PageHelper.orderBy(orderBy);
        }
        cmsFloatBoxMapper.findByContent(content);
        PageInfo<CmsFloatBox> info = new PageInfo<CmsFloatBox>(page);
        return info;
    }

    @Override
    public List<CmsFloatBox> getBox() {
        List<CmsFloatBox> all = cmsFloatBoxMapper.findAll();
//        if (!CollectionUtils.isEmpty(all)) {
//            return all.get(0);
//        }
        return all;
    }
}
